<template>
  <div :id="id" ref="boxwrap" :class="className" :style="{height:height,width:width}" :data="data" />
</template>

<script>
import resize from '@/components/Charts/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    data: {
      type: Object,
      default: () => { return {} }
    }
  },
  data() {
    return {
      chart: null,
      fontColor: '#bebebe',
      color: ['#58D9F9', 'lightgreen', 'orange', 'yellow']
    }
  },
  watch: {
    data: {
      immediate: true,
      handler: function(newval, oldval) {
        this.data = newval
        if (this.chart) {
          this.chart.dispose()
          this.chart = null
          this.initChart()
        }
      },
      deep: true
    }
  },
  mounted() {
    this.domResize()
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      console.log(this.data)
      const tempList = Object.values(this.data.datas)
      let legend = []
      const series = []
      let arr1 = []
      let arr2 = []
      console.log(tempList)
      tempList.forEach(item => {
        arr1 = []
        arr2 = []
        legend = []
        legend.push(item.lineName)
        legend.push(item.lineName + '贡献')
        this.data.xAxis.forEach(type => {
          item.list.forEach(elm => {
            if (type === elm.name) {
              arr1.push(elm.typeCount)
              arr2.push(elm.rate)
            }
          })
        })
        series.push({
          name: item.lineName,
          type: 'bar',
          smooth: true,
          barGap: 0,
          label: {
            show: false,
            position: 'top',
            color: this.fontColor
          },
          data: arr1
        },
        {
          name: item.lineName + '贡献',
          type: 'line',
          smooth: true,
          yAxisIndex: 1,
          zIndex: 2,
          label: {
            show: true,
            position: 'top',
            color: this.fontColor,
            formatter: '{c}%'
          },
          data: arr2
        })
      })
      console.log(series)
      this.chart = this.$echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        color: this.color,
        title: {
          top: 10,
          left: 'center',
          text: '报警前十大贡献度',
          textStyle: {
            fontSize: 16,
            color: this.fontColor
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          top: 10,
          data: legend,
          right: '3%',
          textStyle: {
            fontSize: 14,
            color: this.fontColor
          }
        },
        grid: {
          top: '20%',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          boundaryGap: true,
          axisLabel: {
            color: this.fontColor,
            interval: 0
          },
          data: this.data.xAxis
        }],
        yAxis: [{
          type: 'value',
          name: '次数',
          minInterval: 1,
          nameTextStyle: {
            color: this.fontColor
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            color: this.fontColor
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255,255,255,0.1)'
            }
          }
        }, {
          type: 'value',
          name: '贡献度',
          nameTextStyle: {
            color: this.fontColor
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            color: this.fontColor,
            formatter: '{value}%'
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255,255,255,0.1)'
            }
          }
        }],
        series: series
      })
    },
    domResize() {
      const that = this
      const dom = this.$refs.boxwrap
      this.$erd.listenTo(dom, function(element) {
        that.chart.resize()
      })
    }
  }
}
</script>
